package com.useeinfo.action;

import com.alibaba.fastjson.JSONObject;
import com.useeinfo.module.ContactUs;
import com.useeinfo.service.IContactUs;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * Created by xianping on 2017/1/20.
 */
@Controller
@RequestMapping("/web/contactUs")
public class ContactUsAction {

    @Autowired
    private IContactUs contactUsService;

    @RequestMapping(value = "/addContact",produces ="text/html;charset=UTF-8")
    @ResponseBody
    public String addContact(ContactUs contactUs){
        System.out.println("---------------addContact  start----------------");

        try {
            contactUsService.insertContactUs(contactUs);
        }catch(Exception e){
            e.printStackTrace();
            return "error";
        }
        System.out.println("---------------addContact  end----------------");
        return "success";
    }

	@RequestMapping(value = "/addContactUs",produces ="text/html;charset=UTF-8")
	@ResponseBody
	public String addContactUs(@RequestParam(value = "name", required = false) String name,
							  @RequestParam(value = "telephone", required = false) String telephone,
							  @RequestParam(value = "email", required = false) String email,
							  @RequestParam(value = "content", required = false) String content,
							  HttpServletRequest request){
		System.out.println("---------------addContact2  start----------------");
		JSONObject jsonObject = new JSONObject();
		if(StringUtils.isEmpty(name)
				||StringUtils.isEmpty(telephone)
				||StringUtils.isEmpty(email)
				||StringUtils.isEmpty(content)){
			jsonObject.put("errorCode","1");
			jsonObject.put("errorMsg","信息不能为空");
			return jsonObject.toJSONString();
		}

		ContactUs contactUs = new ContactUs();
		contactUs.setName(name);
		contactUs.setTelephone(telephone);
		contactUs.setEmail(email);
		contactUs.setContent(content);
		try {
			contactUsService.insertContactUs(contactUs);
		}catch(Exception e){
			e.printStackTrace();
			jsonObject.put("errorCode","2");
			jsonObject.put("errorMsg", "插入信息失败");
			return jsonObject.toJSONString();
		}
		jsonObject.put("errorCode","0");
		jsonObject.put("errorMsg","成功");
		System.out.println("---------------addContact2  end----------------");
		return jsonObject.toJSONString();
	}
}
